package umd.mhealth.lab3;


import umd.mhealth.fragmentlab.R;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class MyFragment extends Fragment {
	private ViewGroup rootView;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);

		rootView = (ViewGroup) inflater.inflate(R.layout.my_fragment, container, false);

		final Button buttonOne = (Button) rootView.findViewById(R.id.button_one);
		buttonOne.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				View tv = (View) rootView.findViewById(R.id.tv);
				tv.setBackgroundColor(Color.BLUE);
			}
		});


		final Button buttonTwo = (Button) rootView.findViewById(R.id.button_two);
		buttonTwo.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				View tv = (View) rootView.findViewById(R.id.tv);
				tv.setBackgroundColor(Color.RED);
			}
		});


		if (savedInstanceState != null){
			int bgColor = savedInstanceState.getInt("bgColor");
			View tv = (View) rootView.findViewById(R.id.tv);
			tv.setBackgroundColor(bgColor);
		}

		return rootView;
	}

	@Override
	public void onSaveInstanceState(Bundle savedInstanceState){
		super.onSaveInstanceState(savedInstanceState);
		View tv = (View) rootView.findViewById(R.id.tv);
		int bgColor = Color.TRANSPARENT;
		Drawable bg = (Drawable) tv.getBackground();
		if (bg instanceof ColorDrawable)
			bgColor = ((ColorDrawable) bg).getColor();
		savedInstanceState.putInt("bgColor", bgColor);
	}
}
